      SUBROUTINE BALGEN (N,MA,A,MB,B,LOW,IGH,CSCALE,CPERM,WK)
CSE
C
CPS        PURPOSE:
C
C             BALANCES THE MATRICES A AND B FOR COMPUTATION
C             BY QZ-TYPE ALGORITHMS OF THE EIGENSYSTEM OF THE
C             GENERALIZED EIGENVALUE PROBLEM A*X = (LAMBDA)*B*X .
C
CPE
CAS             ***  ARGUMENT LIST  ***
C
C  INPUT PARAMETERS:
C
C  N - THE ORDER OF THE MATRICES A AND B;
C
C  MA - THE ROW (FIRST) DIMENSION OF THE ARRAY A AS SPECIFIED
C       IN THE CALLING PROGRAM;
C
C  A - A REAL TWO-DIMENSIONAL ARRAY WITH ROW DIMENSION MA AND COLUMN
C      DIMENSION AT LEAST N CONTAINING THE MATRIX A AS THE PROBLEM
C      IS DEFINED IN THE FUNCTION STATEMENT ABOVE;
C
C  MB - THE ROW (FIRST) DIMENSION OF THE ARRAY B AS SPECIFIED IN
C       THE CALLING PROGRAM;
C
C  B - A REAL TWO-DIMENSIONAL ARRAY WITH ROW DIMENSION MB AND COLUMN
C      DIMENSION AT LEAST N CONTAINING THE MATRIX B AS THE PROBLEM
C      IS DEFINED IN THE FUNCTION STATEMENT ABOVE;
C
C  WK - AN ARRAY OF ANY DIMENSION THAT MUST BE PROVIDED BY THE
C       USER FOR WORK SPACE. WK MUST CONTAIN AT LEAST 6*N STORAGE
C       LOCATIONS.
C
C  OUTPUT PARAMETERS:
C
C  A - CONTAINS THE BALANCED A MATRIX;
C
C  B - CONTAINS THE BALANCED B MATRIX;
C
C  LOW - THE INTEGER SPECIFYING THE BEGINNING INDEX OF THE SUBMATRICES
C        OF A AND B CONTAINING THE NON-ISOLATED EIGENVALUES;
C
C  IGH - THE INTEGER SPECIFYING THE ENDING INDEX OF THE SUBMATRICES
C        OF A AND B CONTAINING THE NON-ISOLATED EIGENVALUES. IF
C        IGH=1 (THEN LOW MUST BE 1 ALSO), THE A AND B MATRICES
C        HAVE BEEN PERMUTED INTO UPPER TRIANGULAR FORM AND HAVE
C        NOT BEEN BALANCED;
C
C  CSCALE - THE REAL ARRAY OF DIMENSION AT LEAST N CONTAINING THE
C           EXPONENTS OF THE COLUMN SCALING FACTORS IN ITS LOW THROUGH
C           IGH LOCATIONS AND THE REDUCING COLUMN PERMUTATIONS IN ITS
C           FIRST LOW-1 AND ITS IGH+1 THROUGH N LOCATIONS;
C
C  CPERM - THE REAL ARRAY OF DIMENSION AT LEAST N CONTAINING IN
C          ITS LOW THROUGH IGH LOCATIONS THE COLUMN PERMUTATIONS
C          APPLIED IN GRADING THE A AND B SUBMATRICES;
C
C  WK - CONTAINS THE EXPONENTS OF THE ROW SCALING FACTORS IN ITS
C       LOW THROUGH IGH LOCATIONS, THE REDUCING ROW PERMUTATIONS
C       IN ITS FIRST LOW-1 AND ITS IGH+1 THROUGH N LOCATIONS, AND
C       THE ROW PERMUTATIONS APPLIED IN GRADING THE A AND B SUBMATRICES
C       IN ITS N+LOW THROUGH N+IGH LOCATIONS.
C
CAE
C  REQUIRED SUBPROGRAMS - REDUCE,SCALEG,GRADEQ
C
C  REQUIRED FUNCTIONS - NONE
C
C  REFERENCE - R.C. WARD, BALANCING THE GENERALIZED EIGENVALUE
C              PROBLEM, TECHNICAL REPORT ORNL/CSD-47, OAK RIDGE,
C              TENNESSEE, 1979.
C
C              [SAME TITLE] SIAM J. SCI. STAT. COMPUT., VOL 2, 
C                           NO 2, JUNE 1981, PP 141-152.
C
C ****
C
      DIMENSION A(MA,N),B(MB,N),CSCALE(N),CPERM(N),WK(N,6)
      DOUBLE PRECISION A,B,CSCALE,CPERM,WK
      CALL REDUCE (N,MA,A,MB,B,LOW,IGH,CSCALE,WK)
      IF (LOW .EQ. IGH) GO TO 5
      CALL SCALEG (N,MA,A,MB,B,LOW,IGH,CSCALE,CPERM,WK)
      CALL GRADEQ (N,MA,A,MB,B,LOW,IGH,CPERM,WK(1,2))
    5 CONTINUE
      RETURN
      END
